package com.codedancer.campus.common.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTCreationException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * @Author codeworld
 * @Date 2021/6/25
 **/
public class SecurityUtils {

    private static final long EXPIRE_TIME = 7*24*60*60*1000;

    /**
     * 生成一个Jwt的Token
     * @param uuid 对应的用户uuid
     * @return
     * @throws IllegalArgumentException
     * @throws JWTCreationException
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String genToken(String uuid, String password) throws IllegalArgumentException, JWTCreationException, NoSuchAlgorithmException, UnsupportedEncodingException {
        return JWT.create().withAudience(uuid).withExpiresAt(new Date(System.currentTimeMillis() + EXPIRE_TIME)).sign(Algorithm.HMAC256(password));
    }
}
